package com.example.emos.api.controller;

import cn.hutool.json.JSONUtil;
import com.example.emos.api.common.util.R;
import com.example.emos.api.controller.form.InsertHomeForm;
import com.example.emos.api.db.pojo.TbHome;
import com.example.emos.api.service.BusinessService;
import com.example.emos.api.service.HomeService;
import com.example.emos.api.service.TattooService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;

@RestController
@RequestMapping("/home")
@Tag(name = "HomeController" ,description = "首页")
public class HomeController {
    @Autowired
    private TattooService tattooService;

    @Autowired
    private BusinessService businessService;
    @Autowired
    private HomeService homeService;

    @GetMapping("/searchAllUser")
    @Operation(summary = "查询所有用户")
    public R searchAllUser(){
        ArrayList<HashMap> busList = businessService.searchAllBusiness();
        ArrayList<HashMap> tattooList = tattooService.searchAllTattoos();
        HashMap map=new HashMap<String,ArrayList<HashMap>>();
        map.put("busList",busList);
        map.put("tattooList",tattooList);

        return R.ok().put("data",map);
    }
    @GetMapping("searchAllData")
    @Operation(summary = "数据概览")
    public R searchAllData(){
//        HashMap map=new HashMap<String,String>();
//        map.put("allTattoos","500");
//        map.put("allBusiness","308");
//        map.put("allOrder","666");
//        map.put("allTransaction","8889914.55");
//        map.put("preOrder","6666");
//        map.put("mTattoos","111");
//        map.put("mBusiness","222");
//        return R.ok().put("data",map);
      ArrayList<HashMap> list= homeService.searchAllHome();
      if(list.size()>0){

          return R.ok().put("data",list.get(0));
      }else{
          return R.ok().put("data",new HashMap());
      }
    }

@PostMapping("/updateHome")
@Operation(summary = "同步首页配置")
public R updateHome(@Valid @RequestBody InsertHomeForm insertHomeForm){
    TbHome tbHome= JSONUtil.parse(insertHomeForm).toBean(TbHome.class);
//    tbHome.replace("id",1);
    tbHome.setId(1);

//       homeService.searchAllHome();
    int row=  homeService.updateByPrimaryKeySelective(tbHome);
    return R.ok().put("data",row);
}
}
